package ui;

import entity.EntityContext;
import entity.Question;
import entity.QuestionInfo;
import entity.User;
import service.ContentContext;
import service.IdOrPwdException;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.List;
import java.util.TimerTask;

public class ClientContext {

    private Welcome welcome;
    private Login login;
    private Menu menu;
    private ContentContext content;
    private Exam exam;
    private int id;
    private String pwd;
    private User user;
    //private int num=1;
    //private String examType="综合测试";
    //private String startTime="2021/8/18--2021/8/20";
    private static int time = 5;
    private static int examTime;
    private Timer timer;


    public void showMe() throws InterruptedException {
        int value=(Integer)(100/time);
        int num=0;
        welcome.setVisible(true);
        while (time>0){
            num+=value;
            welcome.setProgressbarValue(num+time);
            time--;
            Thread.sleep(1000);
        }
        login.setVisible(true);
        welcome.setVisible(false);
    }
    public void login(){
        try {
            this.id = login.getUserId();
            this.pwd=login.getUserPwd();
            user=content.loginUser();
            menu.setVisible(true);
            login.setVisible(false);
            menu.updateView(user.getName());
        } catch (IdOrPwdException e) {
            login.showError(e.getMessage());
        }
        catch (NumberFormatException e){
            login.showError("需要输入整数！");
        }
        /*if (user!=null){
            menu.setVisible(true);
            login.setVisible(false);
            menu.updateView(user.getName());
       }
       else {
           menu.loginError();
       }*/
    }


    public void start() throws IOException, InterruptedException {
        exam.setVisible(true);
        menu.setVisible(false);
        QuestionInfo qu=content.start();
        examTime =content.getInformation().getTimeLimit();
        exam.updateQuestion(qu.getQuestion());
        timer = new Timer(1000, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (examTime<=0){
                    menu.setVisible(true);
                    exam.setVisible(false);
                    examEnd();
                }
                examTime--;
                int s=examTime%60;
                int m=examTime/60;
                String str="剩余时间:"+m+"分钟"+s+"秒";
                exam.updateView(user.getName(), user.getId(), content.getInformation().getTitle(),
                        content.getInformation().getStartTime(), str, content.getInformation().getQuestionCount());
            }
        });
        exam.updateView(user.getName(), user.getId(), "XX测试",
                "2021/8/18--2021/8/20", "剩余时间:00分00秒", 1);
        timer.start();

        /*timer.schedule(new TimerTask() {
            public void run() {
                examTime--;

            }
        },1000);*/
    }
    public void nextQues() {
            QuestionInfo qu=content.nextQues();
            exam.updateQuestion(qu.getQuestion());
    }
    public void prevQues() {
            QuestionInfo qu=content.prevQues();
            exam.updateQuestion(qu.getQuestion());
    }


    public void setLogin(Login login) {
        this.login = login;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public void setContent(ContentContext content) {
        this.content = content;
    }



    public int getId() {
        return id;
    }

    public void setWelcome(Welcome welcome) {
        this.welcome = welcome;
    }

    public String getPwd() {
        return pwd;
    }

    public void setExam(Exam exam) {
        this.exam = exam;
    }

    public String getRule() {
        return  content.getRule();
    }


    public void subAnswer(List<Integer> userAnswer) {
        content.subAnswer(userAnswer);
    }

    public void countScore() {
        content.countScore();
    }

    public int getScore() {
        return content.getScore();
    }

    public void backMuen() {
        exam.setVisible(false);
        menu.setVisible(true);
    }

    public void examEnd() {
        timer.stop();
        time=0;
        //提交试卷后无法进入考场,去掉该功能，将会重新分发一张考卷,之前的考试数据清空
        menu.updateExamView();
    }

    public void prevTitle() {
        exam.firstTitle();
    }

    public void nextTitle() {
        exam.endTitle();
    }

    public void backLogin() {
        login.setVisible(true);
        menu.setVisible(false);
    }
}
